package lu.med.praxiswahl.wahlumed.service.beans;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.*;
import lu.med.praxiswahl.wahlumed.model.beans.BufferTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.BufferServiceInt;

@Stateless
@Remote( { BufferServiceInt.class })
public class BufferService implements BufferServiceInt {

	private BufferTBL buffer;
	
	@EJB
	private BufferDAOInt buffDao;

	public BufferService() {
	buffer = new BufferTBL();
	}

	@Override
	public void clearBufferTbl() {

		buffDao.delete("CONSULTINGROOM");
		buffDao.delete("LABOR");
	}

	@Override
	public String getCustomerInConsultingRoom() {
		String tmpBuff = buffDao.getCustomerInConsultingRoom();
		return tmpBuff;
	}

	@Override
	public String getCustomerInLab() {
		String tmpBuff = buffDao.getCustomerInLab();
		System.out.println(tmpBuff);
		return tmpBuff;
	}

	@Override
	public void setCustomerInConsultingRoom(String cust) {

		String typ = "CONSULTINGROOM";
		BufferTBL tmpBuff = buffDao.findTypByID(typ);

		BufferTBL tmpBuffLab = buffDao.findTypByID("LABOR");

		if (tmpBuff != null) {
			if (tmpBuffLab != null) {
				if (cust.equals(tmpBuffLab.getMatricule())) {
					this.clearLab();

				}

			}

			buffer.setTyp(tmpBuff.getTyp());
			buffer.setMatricule(cust);
			buffDao.update(buffer);
		} else {
			if (tmpBuffLab != null) {
				if (cust.equals(tmpBuffLab.getMatricule())) {
					this.clearLab();

				}
				buffer.setTyp(typ);
				buffer.setMatricule(cust);
				buffDao.persist(buffer);
			}
		}

		buffer = null;
		buffer = new BufferTBL();

	}

	@Override
	public void setCustomerInLab(String cust) {

		String typ = "LABOR";
		BufferTBL tmpBuff = buffDao.findTypByID(typ);

		if (tmpBuff != null) {
			buffer.setTyp(tmpBuff.getTyp());
			buffer.setMatricule(cust);
			buffDao.update(buffer);
		} else {
			buffer.setTyp(typ);
			buffer.setMatricule(cust);
			buffDao.persist(buffer);
		}

		buffer = null;
		buffer = new BufferTBL();

	}

	@Override
	public void clearConsultingRoom() {
		this.buffDao.delete("CONSULTINGROOM");

	}

	@Override
	public void clearLab() {
		this.buffDao.delete("LABOR");

	}

}
